export const comparedArr = (old, update) => {
  let res = true
  // 先比较长度 
  if (old.length != update.length) {
    res =  false
  }
  // 再比较id（根据数组中对象的某个属性比较）
  let oldIds = old.map( (item) => {
    return item
  })
  let updateIds = update.map( (item) => {
    return item
  })
  for (let i = 0, len = oldIds.length; i < len; i++) {
    if (updateIds.indexOf(oldIds[i]) == -1) {
      res = false
    }
  }
  return res
}

export const throttle = (fn, wait) => {
  // console.log('wait', wait);
  let timer;
  return function () {
      if (!timer) {
          timer = setTimeout(() => {
              timer = null;
              fn.apply(this, arguments);
          }, wait)
      }
  }
}


export const debounce = (fn,wait)=>{
  var timer = null;
  return function(){
      if(timer !== null){
          clearTimeout(timer);
      }
      timer = setTimeout(fn,wait);
  }
}


export const removeListItem = (arr,item)=>{
  return arr.filter(f => f != item)
}


export const IsPrintTool = (data, UserId) => {
    if(data.PullNumber) {
      let PullNumberList = data.PullNumber.split(',')
      PullNumberList.forEach(item => {
        let printParams = {
          PullNumber: item,
          DeliveryId: data.DeliveryId,
          FlowNo: data.FlowNo,
          TaskId: data.TaskId,
        }
        PrintBarcode(printParams, UserId)
      });
    }
}

  // 打印条码
  const  PrintBarcode = (printParams, UserId) => {

    // 存储区不能为空，且需要扫描指定订单后才能打印条码。
    var url = "ws://localhost:13145/Print?UserId=" + UserId;
    let TaskId = printParams.PullNumber + '_' + Date.parse(new Date()).toString();
    let params = {
      DeliveryId:printParams.DeliveryId,
      PullNumber:printParams.PullNumber,
      TaskId:'',
      FlowNo:'',
      PrintType:1
    }
    var command = [{
              TaskId,
              WarehouseId: '06',
              StorerId: 'WCD',
              MenuId: "M891",
              Action: "Print",
              DataSourceParameter: JSON.stringify(params)
              // DataSourceParameter: "{'DeliveryId':'" + (printParams.DeliveryId || '').toString() + "','PullNumber':'" + (printParams.PullNumber || '').toString() + "','TaskId':'" + (printParams.TaskId || '').toString() + "','FlowNo':'" + (printParams.FlowNo || '').toString() + "'}"
            }];
    
    SendCommand(url, command);
  }



export const PrintInit = async(UserId) => {
  var url = "ws://localhost:13145/Login?UserId=" + UserId;
  var command = {
    RemoteUrl: sessionStorage.getItem("RemoteUrl"),

  };
  console.log('ddddd', "异步登录打印机");
  SendCommand(url, command);
}

const SendCommand = (url, command) => {
  var ws = new WebSocket(url);
  ws.onopen = function () {
    ws.send(JSON.stringify(command));
  };
  ws.onmessage = function (e) {
    //alert(e.data);
    console.log('onmessage',e.data);
    // document.getElementById("response").innerText = e.data;
  };
  ws.onclose = function () {
    //alert("close");
  };
}